#include "VFSManager.h"
#include <physfs.h>
#include <iostream>
#include "StringUtil.h"

namespace PhysFS {

VFSManager* VFSManager::instance;

VFSManager::VFSManager(const char* arg0)
{
	if(VFSManager::instance)
		throw std::exception("VFSManager is created twice");
	VFSManager::instance=this;
	PHYSFS_init(arg0);
	//default setting
	mount(PHYSFS_getBaseDir());
	setWriteDir(PHYSFS_getBaseDir());
}

VFSManager::~VFSManager()
{
	PHYSFS_deinit();
}

VFSManager& VFSManager::getInstance()
{
	return *VFSManager::instance;
}

bool VFSManager::mount(const char* path, const char* mountPoint, bool append)
{
	bool ret=PHYSFS_mount(path,mountPoint,append)!=0;
	if(!ret)
		printf(PHYSFS_getLastError());
	return ret;
}

bool VFSManager::wmount(const wchar_t* path, const wchar_t* mountPoint, bool append)
{
	return mount(TOUTF8(path).c_str(), TOUTF8(mountPoint).c_str(), append);
}
	
bool VFSManager::wunmount(const wchar_t* path)
{
	return unmount(TOUTF8(path).c_str());
}

bool VFSManager::unmount(const char* path)
{
	bool ret=PHYSFS_removeFromSearchPath(path)!=0;
	if(!ret)
		printf(PHYSFS_getLastError());
	return ret;
}

bool VFSManager::setWriteDir(const char* path)
{
	bool ret=PHYSFS_setWriteDir(path)!=0;
	if(!ret)
		printf(PHYSFS_getLastError());
	return ret;
}

unsigned long long VFSManager::getModifiedDate(const char* path)
{
	return PHYSFS_getLastModTime(path);
}

bool VFSManager::exists(const char* path)
{
	return PHYSFS_exists(path)!=0;
}

}